﻿namespace GCC.JobContainer
{
    internal static class SystemChecker
    {
        /// <summary>
        /// 系统检查
        /// </summary>
        public static bool Check(
            out int globalDBType
            , out int localDBType
            , out string globalDBAddress
            , out string globalReadDBAddress
            , out string localDBAddress
            , out string localReadDBAddress
            , out string pointName
            , out string authCenter
            , out string configCenter
            )
        {
            var res = true;
            localDBType = globalDBType = 0;
            Console.WriteLine("Start to check system environment variable");

            var globalDBTypeStr = Environment.GetEnvironmentVariable("globalDBType") ?? "";
            var localDBTypeStr = Environment.GetEnvironmentVariable("localDBType") ?? "";
            globalDBAddress = Environment.GetEnvironmentVariable("globalDB") ?? "";
            globalReadDBAddress = Environment.GetEnvironmentVariable("globalReadDB") ?? "";
            localDBAddress = Environment.GetEnvironmentVariable("localDB") ?? "";
            localReadDBAddress = Environment.GetEnvironmentVariable("localReadDB") ?? "";
            pointName = Environment.GetEnvironmentVariable("GCC.PointName") ?? "";
            if (string.IsNullOrWhiteSpace(pointName))
            {
                Console.WriteLine(Environment.GetEnvironmentVariable("GCCPointName"));
                pointName = Environment.GetEnvironmentVariable("GCCPointName") ?? "";
            }
            authCenter = Environment.GetEnvironmentVariable("AuthCenter") ?? "";
            configCenter = Environment.GetEnvironmentVariable("ConfigCenter") ?? "";

            if (!string.IsNullOrWhiteSpace(globalDBTypeStr) && !int.TryParse(globalDBTypeStr, out globalDBType))
            {
                Console.WriteLine("globalDBType set but it's value is not int, system will shutdown.");
                res = false;
            }
            if (!string.IsNullOrWhiteSpace(localDBTypeStr) && !int.TryParse(localDBTypeStr, out localDBType))
            {
                Console.WriteLine("localDBType set but it's value is not int, system will shutdown.");
                res = false;
            }

            if (string.IsNullOrWhiteSpace(globalDBAddress))
            {
                Console.WriteLine("globalDBAddress not set, we will init null value.");
            }

            if (string.IsNullOrWhiteSpace(globalReadDBAddress))
            {
                Console.WriteLine("globalReadDBAddress not set, we will init null value.");
            }
            if (string.IsNullOrWhiteSpace(localDBAddress))
            {
                Console.WriteLine("localDBAddress not set, we will init null value.");
            }
            if (string.IsNullOrWhiteSpace(localReadDBAddress))
            {
                Console.WriteLine("localReadDBAddress not set, we will init null value.");
            }

            if (string.IsNullOrWhiteSpace(pointName))
            {
                Console.WriteLine("pointName not set, system will shutdown.");
                res = false;
            }

            if (string.IsNullOrWhiteSpace(authCenter))
            {
                Console.WriteLine("authCenter not set, system will shutdown.");
                res = false;
            }

            if (string.IsNullOrWhiteSpace(configCenter))
            {
                Console.WriteLine("configCenter not set, system will shutdown.");
                res = false;
            }

            if (string.IsNullOrWhiteSpace(localDBAddress) && string.IsNullOrWhiteSpace(localReadDBAddress))
            {
                Console.WriteLine("All local db was null, system will shutdown");
                res = false;
            }

            return res;
        }
    }
}
